﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using System.Windows.Forms;
using System.Security.Cryptography;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using System.Data;
using CdcSoftware.Pivotal.Engine.Types.Database;
using CdcSoftware.Pivotal.Engine.Types.MailMerges;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Applications.Core.Common;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_Sale_Serial_Number : FormClientTask
    {
        /// <summary>
        /// On Exit of Barcode field.
        /// </summary>
        /// <param name="sender">The control associated with the event triggered</param>
        /// <param name="args">The argument for the event</param>
        /// <history>
        /// #Revision   Date            Author      Description
        /// 6.0.12.1    22-02-2013      AS          Initial version.
        /// </history>
        public virtual void onExitBarcode(object sender, EventArgs args)
        {
            try
            {
                if (!Convert.IsDBNull(this.PrimaryDataRow["Product_Barcode"]))
                {
                    DataTable product = Globals.GetDataTable("RE_Product for Barcode? Alt Code1? Alt Code2? Alt Code3?",
                           new object[] { this.PrimaryDataRow["Product_Barcode"], this.PrimaryDataRow["Product_Barcode"], 
                        this.PrimaryDataRow["Product_Barcode"], this.PrimaryDataRow["Product_Barcode"]}, "Product_Id");
                    if (product != null && product.Rows.Count == 1)
                        this.PrimaryDataRow["Product_Id"] = product.Rows[0]["Product_Id"];
                    else
                    {
                        this.PrimaryDataRow["Product_Barcode"] = DBNull.Value;
                        PivotalMessageBox.Show("Invalid product barcode.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex);
            }
        }
    }    
}
